package com.hxd.mall.module.account.register

import android.graphics.Color
import android.text.SpannableString
import android.text.TextUtils
import android.text.method.LinkMovementMethod
import com.alibaba.android.arouter.facade.annotation.Route
import com.hxd.mall.R
import com.hxd.mall.base.BaseInjectActivity
import com.hxd.mall.constant.HxdConstants
import com.hxd.mall.constant.HxdConstants.IntentParamsKeyDefine.login_bind_view
import com.hxd.mall.constant.HxdConstants.IntentParamsKeyDefine.login_or_register_phone_num
import com.hxd.mall.constant.HxdConstants.IntentParamsKeyDefine.login_or_register_type
import com.hxd.mall.constant.HxdConstants.IntentParamsKeyDefine.register_invite_code
import com.hxd.mall.constant.HxdConstants.Tag_Cancel
import com.hxd.mall.constant.HxdConstants.Tag_Confirm
import com.hxd.mall.constant.HxdConstants.from_register_to_verifyCode
import com.hxd.mall.constant.HxdConstantsNet.userAgreementUrl
import com.hxd.mall.constant.HxdConstantsNet.userPrivacyAgreementUrl
import com.hxd.mall.constant.HxdConstantsNet.userServiceAgreementUrl
import com.hxd.mall.constant.HxdConstantsRouterPath.PathAccount.path_register
import com.hxd.mall.constant.HxdConstantsRouterPath.PathAccount.path_verify_code
import com.hxd.mall.module.account.bean.InvitationCodeBean
import com.hxd.mall.utils.local.NavigationUtil.jumpToWebView
import com.hxd.mall.utils.local.NavigationUtil.routerNavigation
import com.hxd.mall.utils.local.inviting_code_input_box_click
import com.hxd.mall.utils.local.inviting_code_page_next_button_click
import com.hxd.mall.utils.local.inviting_code_page_view
import com.hxd.mall.utils.local.sendPoint
import com.hxd.mall.utils.open.StringUtil.setSpanClick
import kotlinx.android.synthetic.main.activity_register_layout.*

/**
 * @author zhaoyong
 * @description: 邀请码输入页
 * @date :2019-10-15 15:26
 */
@Route(path = path_register)
class RegisterActivity : BaseInjectActivity<IRegisterPresenter>(), IRegisterView {

    private lateinit var phoneNum: String

    /**
     * 邀请码是否输入完成
     */
    private var inputFinish = false
    /**
     * 勾选框是否勾选
     */
    private var boxSelected = true

    /**
     * 1 正常注册流程
     *
     * 当前类型来源
     */
    private var mCurrentType = 1

    override fun attachLayoutRes(): Int = R.layout.activity_register_layout

    override fun initViews() {
        setTitleCommon()
        inputInviteCode.inputFinish { finish ->
            inputFinish = finish
            changeNextBtnStatus()

            sendPoint(inviting_code_input_box_click)
        }

        next.setOnClickListener {
            if (!TextUtils.isEmpty(inputInviteCode.getInputContent())) {
                mPresenter?.invitationCode(inputInviteCode.getInputContent())
            }

            sendPoint(inviting_code_page_next_button_click)
        }
        //设置了setOnClickListener后，强制clickable为true,xml中的false失效，需要在代码中设置
        next.isClickable = false
        initAgreementView()

        sendPoint(inviting_code_page_view)
    }

    override fun initData() {
        phoneNum = intent.getStringExtra(login_or_register_phone_num) ?: ""
        mCurrentType = intent.getIntExtra(HxdConstants.IntentParamsKeyDefine.login_bind_view, 1)
    }

    override fun invitationCodeResult(t: InvitationCodeBean) {
        routerNavigation(
            path_verify_code, hashMapOf(
                login_or_register_phone_num to phoneNum,
                login_or_register_type to from_register_to_verifyCode,
                register_invite_code to inputInviteCode.getInputContent(),
                login_bind_view to mCurrentType
            )
        )
    }

    private fun initAgreementView() {
        box.tag = Tag_Confirm
        box.setOnClickListener {
            if (box.tag == Tag_Cancel) {
                box.tag = Tag_Confirm
                box.setImageResource(R.mipmap.select)
                boxSelected = true
            } else {
                box.tag = Tag_Cancel
                box.setImageResource(R.mipmap.unselect)
                boxSelected = false
            }
            changeNextBtnStatus()
        }

        agreement.run {
            movementMethod = LinkMovementMethod.getInstance()
            text = SpannableString(getString(R.string.user_agreenment)).apply {
                setSpanClick(this, 8, 21) {
                    jumpToWebView(getString(R.string.app_user_agreement), userAgreementUrl)
                }
                setSpanClick(this, 23, 29) {
                    jumpToWebView(getString(R.string.user_privacy_agreement), userPrivacyAgreementUrl)
                }
                setSpanClick(this, 31, 37) {
                    jumpToWebView(getString(R.string.user_service_agreement), userServiceAgreementUrl)
                }
            }
        }
    }

    private fun changeNextBtnStatus() {
        if (inputFinish && boxSelected) {
            next.isClickable = true
            next.setBackgroundResource(R.drawable.shape_btn_disable_bg)
        } else {
            next.isClickable = false
            next.setBackgroundColor(Color.parseColor("#55ffffff"))
        }
    }
}